﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class DictionaryController : BaseController
    {
        BPMS_ItemsIBLL bpms_itemsibll = new BPMS_ItemsBLL();
        BPMS_Items bpms_items = new BPMS_Items();
        BPMS_ItemDetailsIBLL bpms_itemdetailsibll = new BPMS_ItemDetailsBLL();
        BPMS_ItemDetails bpms_itemdetails = new BPMS_ItemDetails();

        #region 辅助资料管理
        /// <summary>
        /// 辅助资料管理
        /// </summary>
        /// <returns></returns>
        public ActionResult DictionaryIndex()
        {
            return View();
        }
        #endregion

        #region 辅助资料类别列表
        /// <summary>
        /// 辅助资料类别列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ItemsList()
        {
            ViewData["strHtml"] = GetItemsTree();
            return View();
        }
        int index = 0;
        string strclass = "";
        /// <summary>
        /// 数据字典主表
        /// </summary>
        public string GetItemsTree()
        {
            StringBuilder strHtml = new StringBuilder();
            List<BPMS_Items> list = IListHelper.IListToList<BPMS_Items>(bpms_itemsibll.GetList());
            IList<BPMS_Items> listnew = list.Distinct(new Comparint<BPMS_Items>("Category")).ToList();
            foreach (BPMS_Items entity in listnew)
            {
                strHtml.Append("<li>");
                strHtml.Append("<div>");
                strHtml.Append("<img src='/Themes/images/16/advancedsettings.png' style='vertical-align: middle;' alt=''/>" + entity.Category + "");
                strHtml.Append("</div>");
                strHtml.Append("<ul>");
                //创建子节点
                strHtml.Append(GetTreeNode(entity.Category, list));
                strHtml.Append("</ul>");
                strHtml.Append("</li>");
            }
            return strHtml.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNode(string Category, IList list)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Items> itemNode = IListHelper.IListToList<BPMS_Items>(list).FindAll(t => t.Category == Category);
            foreach (BPMS_Items entity in itemNode)
            {
                if (index == 0)
                    strclass = "collapsableselected";
                else
                    strclass = "";
                sb_TreeNode.Append("<li>");
                sb_TreeNode.Append("<div title='" + entity.Code + "' class='" + strclass + "' id='" + entity.ItemsId + "'><img src='/Themes/images/16/wrench.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                sb_TreeNode.Append("</li>");
                index++;
            }
            return sb_TreeNode.ToString();
        }
        /// <summary>
        /// 删除字典类别
        /// </summary>
        public ActionResult DeleteItems(string key)
        {
            bpms_items = bpms_itemsibll.GetEntity(key);
            if (bpms_items.AllowDelete == 0)
            {
                Response.Write(string.Format(MessageHelper.MSG0009, bpms_items.FullName));
                Response.End();
            }
            else if (bpms_itemsibll.IsItemDetails(key))
            {
                bpms_items = bpms_itemsibll.GetEntity(key);
                Response.Write(string.Format(MessageHelper.MSG0023, bpms_items.FullName));
                Response.End();
            }
            else
            {
                Response.Write(bpms_itemsibll.Delete(key));
                Response.End();
            }
            return null;
        }
        #endregion

        #region 辅助资料类别表单
        /// <summary>
        /// 辅助资料类别表单
        /// </summary>
        /// <returns></returns>
        public ActionResult ItemsForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_items">实体类</param>
        /// <returns></returns>
        public ActionResult AcceptClick(BPMS_Items bpms_items)
        {
            string key = Request["key"];                                                //主键
            bool IsOk = false;
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_items.ItemsId = key;
                bpms_items.ModifyDate = DateTime.Now;
                bpms_items.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_items.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_itemsibll.Update(bpms_items);
            }
            else
            {
                bpms_items.ItemsId = CommonHelper.GetGuid;
                bpms_items.CreateUserId = SessionHelper.GetSessionUser().UserId;
                bpms_items.CreateUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_itemsibll.Insert(bpms_items);
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControl(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_items = bpms_itemsibll.GetEntity(key);
                return Content(JsonHelper.ObjectToJson(bpms_items));
            }
            return null;
        }
        #endregion

        #region 辅助资料明细列表
        /// <summary>
        /// 辅助资料明细列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ItemDetailsList()
        {
            return View();
        }
        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string orderField = Request["pqGrid_OrderField"];                           //排序字段  
            string orderType = Request["pqGrid_OrderType"];                             //排序类型
            string pqGrid_Sort = Request["pqGrid_Sort"];                                //要显示字段
            string key = Request["key"];                                                //主键
            string ItemsId = Request["ItemsId"];                                        //字段类别主键
            switch (active)
            {
                case "GridBindList"://加载列表
                    Response.Write(JsonHelper.PqGridJson<BPMS_ItemDetails>(bpms_itemdetailsibll.GetListByItemsId(ItemsId), pqGrid_Sort));
                    Response.End();
                    break;
                case "Delete":    //删除数据
                    Response.Write(bpms_itemdetailsibll.Delete(key));
                    Response.End();
                    break;
                case "autocomplete"://自动补全，返回JSON
                    Response.Write(JsonHelper.ListToJson<BPMS_ItemDetails>(bpms_itemdetailsibll.GetListByItemsId(ItemsId), "JSON"));
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 辅助资料明细表单
        /// <summary>
        /// 辅助资料明细表单
        /// </summary>
        /// <returns></returns>
        public ActionResult ItemDetailsForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_itemdetails">实体类</param>
        /// <returns></returns>
        public ActionResult AcceptClickDetails(BPMS_ItemDetails bpms_itemdetails)
        {
            string key = Request["key"];                                                //主键
            bool IsOk = false;
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_itemdetails.ItemDetailsId = key;
                bpms_itemdetails.ModifyDate = DateTime.Now;
                bpms_itemdetails.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_itemdetails.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_itemdetailsibll.Update(bpms_itemdetails);
            }
            else
            {
                bpms_itemdetails.ItemDetailsId = CommonHelper.GetGuid;
                bpms_itemdetails.CreateUserId = SessionHelper.GetSessionUser().UserId;
                bpms_itemdetails.CreateUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_itemdetailsibll.Insert(bpms_itemdetails);
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControlDetails(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_itemdetails = bpms_itemdetailsibll.GetEntity(key);
                return Content(JsonHelper.ObjectToJson(bpms_itemdetails));
            }
            return null;
        }
        #endregion
    }
}
